/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.seagate.app.servlet;

import com.seagate.app.manager.LimitManager;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 452530
 */
public class LimitServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
              response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
          if("loadlimit".equalsIgnoreCase(action)){
          LimitManager lm=new LimitManager();
          int limit=lm.getLimit();
          int limit2=lm.getLimit2();
          out.println("<table bgcolor=\"#CCCCCC\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
          out.println("<tr><td>");
          out.println("<table bgcolor=\"#6699CC\" cellpadding=\"3\" cellspacing=\"0\" border=\"0\" width=\"100%\">");

          out.println("<tr bgcolor=\"#eeeeee\">");
          out.println("<td>");
          out.println("<table bgcolor=\"#cccccc\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
          out.println("<tr><td>");
          out.println("<table bgcolor=\"#cccccc\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\" width=\"100%\">");

                 out.println("<tr bgcolor=\"#33FF99\"  >");
                    out.println("<td align=\"center\"> LIMIT </td><td  align=\"center\">Warning</td><td  align=\"center\">Critical</td><td  align=\"center\">Command</td>");
                    out.println(" </tr>");
              out.println("<tr bgcolor=\"#ffffff\">");
               out.println("<td>&nbsp;Alert Limit &nbsp;</td>");
              out.println("<td>&nbsp;"+limit+"&nbsp;</td>");
              out.println("<td>&nbsp;"+limit2+"&nbsp;</td>");





              out.println("<td><input name=\"imgEdit\" id=\"imgEdit\" src=\"./images/buttons/btn_edit.gif\" onclick=\"editLimit('"+limit+"','"+limit2+"')\"  border=\"0\"  type=\"image\"> </td>");

              out.println("</tr>");



          out.println("</table>");
          out.println("</td></tr>");
          out.println("</table>");
          out.println("</td>");
          out.println("</tr>");

          out.println("</table>");
          out.println("</td></tr>");
          out.println("</table>");
          }else if("addlimit".equalsIgnoreCase(action)){
                String limit = request.getParameter("limit");
                String limit2 = request.getParameter("limit2");
                LimitManager lm=new LimitManager();
                boolean success=lm.editLimit(limit,limit2);
                out.println("{\"success\":" + success + "}");
          }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
